﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ExcelFileQueryTool
{
    public sealed class SimpleFileWatcher
    {
        private DateTime _oriModifiedTime;
        private string _file;
        private string _sessionId;

        public SimpleFileWatcher(string file, string sessionId)
        {
            if (string.IsNullOrEmpty(file))
                throw new ArgumentNullException("file");

            if (File.Exists(file) == false)
                throw new FileNotFoundException("文件不存在", file);

            if (string.IsNullOrEmpty(sessionId))
                throw new ArgumentNullException("sessionId");

            _file = file;
            _sessionId = sessionId;
            _oriModifiedTime = File.GetLastWriteTime(file);
        }

        public bool FileHasChanged
        {
            get
            {
                var curModfiedTime = File.GetLastWriteTime(_file);
                return curModfiedTime != _oriModifiedTime;
            }
        }

        public void UpdateModifiedTime(string sessionId)
        {
            if (_sessionId.Equals(sessionId, StringComparison.OrdinalIgnoreCase) == false)
                throw new Exception("会话ID错误，无法更新文件修改时间");

            _oriModifiedTime = File.GetLastWriteTime(_file);
        }
    }
}
